package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.AlertDialog;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.Stack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Stack<Float> stack = new Stack<>();
    private Stack<String> system = new Stack<>();
    private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven, btn_eight,
            btn_nine, btn_zero, btn_point, btn_equal, btn_delete, btn_ac, btn_add, btn_divide, btn_multiply;
    private Button[] buttons = {btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven, btn_eight,
            btn_nine, btn_zero, btn_point, btn_equal, btn_delete, btn_ac, btn_add, btn_divide, btn_multiply};
    private int[] btn_id = {R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8
            , R.id.btn_9, R.id.btn_point, R.id.btn_equal, R.id.btn_del, R.id.btn_ac, R.id.btn_add, R.id.btn_div, R.id.btn_mult};
    private EditText edit, show_edit;
    private boolean opflag = false;
    private float ans;

    private void handleNumberClick(int number) {
        setOpflag();
        edit_append(number);
    }

    private void pushNumToSt() {
        opflag = true;
        if(!edit.getText().toString().isEmpty()){
            float num = Float.parseFloat(edit.getText().toString());
            stack.push(num);
            edit.setText("");
        }
    }

    private void setOpflag(){
        if(opflag){
            opflag = false;
            edit.setText("");
        }
        if(show_edit.getText().toString().indexOf("=") != -1){
            show_edit.setText("");
        }
    }

    private void edit_append(int i){
        edit.append(String.valueOf(i));
        show_edit.append(String.valueOf(i));
    }

    private void edit_append(String sys){
        edit.append(sys);
        show_edit.append(sys);
    }

    private void edit_append_operator(String operator){
        if (edit.getText().toString().isEmpty() || system.size() >= 1) {
            Toast.makeText(MainActivity.this, "每个符号前请输入数字", Toast.LENGTH_LONG).show();
            return;
        }
        System.out.println("2.println");
        pushNumToSt();
        if(!stack.empty()){
            if(show_edit.getText().toString().indexOf("=") != -1){
                show_edit.setText("");
                show_edit.append(String.valueOf(ans));
            }
            system.push(operator);
            edit_append(" " + operator + " ");
        }
    }

    private void calculate(String operator){
        while(!stack.empty()){
            if(operator.equals("+"))
                ans += stack.pop();
            else if(operator.equals("%"))
                ans = stack.pop() / ans;
            else if(operator.equals("×"))
                ans *= stack.pop();
            else if(operator.equals("-"))
                ans = stack.pop() - ans;
        }
        edit.setText(String.valueOf(ans));
        show_edit.append(" " + "=");
    }


    private void findViewID()
    {
        for(int i = 0; i < buttons.length; ++i){
            buttons[i] = findViewById(btn_id[i]);
        }
        show_edit = findViewById(R.id.show_view);
        edit = findViewById(R.id.view);
    }
    private void setOnClick() {
        for (int i = 0; i < buttons.length; ++i) {
            buttons[i].setOnClickListener(this);
            if (i == 14) { // btn_add
                buttons[i].setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        edit_append_operator("-");
                        return true;
                    }
                });
            }
        }
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);
        findViewID();
        setOnClick();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_0:
                handleNumberClick(0);
                break;

            case R.id.btn_1:
                handleNumberClick(1);
                //Toast.makeText(MainActivity.this, "1被点击", Toast.LENGTH_SHORT).show();
                break;

            case R.id.btn_2:
                handleNumberClick(2);
                //Toast.makeText(MainActivity.this, "2被点击", Toast.LENGTH_LONG).show();
                break;

            case R.id.btn_3:
                handleNumberClick(3);
                break;

            case R.id.btn_4:
                handleNumberClick(4);
                break;

            case R.id.btn_5:
                handleNumberClick(5);
                break;

            case R.id.btn_6:
                handleNumberClick(6);
                break;

            case R.id.btn_7:
                handleNumberClick(7);
                break;

            case R.id.btn_8:
                handleNumberClick(8);
                break;

            case R.id.btn_9:
                handleNumberClick(9);
                break;

            case R.id.btn_point:
                if(edit.getText().toString().indexOf(".") == -1){
                    setOpflag();
                    edit_append(".");
                }
                break;

            case R.id.btn_equal:
                opflag = true;
                if(!system.empty() && !stack.empty()){
                    String sys = system.pop();
                    ans = (!edit.getText().toString().equals(" " + sys + " ")) ? Float.parseFloat(edit.getText().toString()) : 0f;
                    if(ans == 0 && edit.getText().toString().equals(" " + sys + " "))
                        edit_append(0);
                    switch (sys){
                        case "+":
                            calculate("+");
                            break;
                        case "%":
                            calculate("%");
                            break;
                        case "×":
                            calculate("×");
                            break;
                        case "-":
                            calculate("-");
                            break;
                    }
                }
                break;

            case R.id.btn_del:
                edit.dispatchKeyEvent(new KeyEvent(
                        KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
                if(show_edit.getText().toString().indexOf("=") == -1)
                    show_edit.dispatchKeyEvent(new KeyEvent(
                            KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
                break;

            case R.id.btn_ac:
                opflag = false;
                edit.setText("");
                show_edit.setText("");
                stack.clear();
                break;

            case R.id.btn_add:
                edit_append_operator("+");
                break;

            case R.id.btn_mult:
                edit_append_operator("×");
                break;

            case R.id.btn_div:
                edit_append_operator("%");
                break;

            default:
                Toast.makeText(MainActivity.this, "乱点", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}